import {Component, OnInit} from '@angular/core';
import {App, NavController, ViewController} from 'ionic-angular';
import {AlertController} from 'ionic-angular';
import {ChildService} from "./child.service";
import {AddChildPage} from "../addChild/addChild";
import {services} from "../../providers/service";
import {UserLogin} from "../../modules/app-user/user-login/user-login";
import {UserService} from "../../providers/user-service";
import {AppPopup} from "../../util/popup";

@Component({
    selector: 'child', templateUrl: 'child.html', providers: [ChildService]
})

export class ChildPage implements OnInit {
    limitCount;
    list;
    count;
    showAlert = true;
    index;
    delInfo = {
        "name": '',
        "mobile": ''
    };

    constructor(public navCtrl: NavController,
                public service: services,
                private userServ: UserService,
                private app: App,
                private popup: AppPopup,
                public childService: ChildService,
                public viewCtrl: ViewController,) {
    }

    ionViewWillEnter() {
        this.userServ.getPersonInfo()
            .then(() => {
            })
            .catch(err => {
                console.error("用户详情获取失败：", err);
                if (err.errorMassage == "未登录或登录超时！") {
                    this.popup.alert(err.errorMassage)
                        .then(() => {
                            this.app.getRootNav().setRoot(UserLogin);
                        });
                }
            });
        this.limitCount = this.service.personalDetail.companyMemberLimit;
        console.log(this.service.personalDetail);
        let param = {}
        this.childService.childList(param)
            .then(data => {
                this.list = data.infoData.items;
                console.log(data.infoData);
            }, err => {
                console.log(err);
                return err;
            })
            .catch(fail => {
                console.log(fail);
            });
        let param2 = {};
        this.childService.childCount(param2)
            .then(data => {
                if (data.resultStatus == "success") {
                    this.count = data.infoData;
                }
            }, err => {
                console.log(err);
            })
            .catch(fail => {
                console.log(fail);
            });

    }

    // didEnter回调
    public onDidEnter() {
        this.limitCount = this.service.personalDetail.companyMemberLimit;
        console.log(this.service.personalDetail);
        let param = {}
        this.childService.childList(param)
            .then(data => {
                this.list = data.infoData.items;
                this.count = data.infoData.count;
                console.log(data);
            }, err => {
                console.log(err);
                return err;
            })
            .catch(fail => {
                console.log(fail);
            });
        let param2 = {};
        this.childService.childCount(param2)
            .then(data => {
                if (data.resultStatus == "success") {
                    this.count = data.infoData;
                }
            }, err => {
                console.log(err);
            })
            .catch(fail => {
                console.log(fail);
            });
    }

    backPersonInfo() {
        this.navCtrl.pop();
    };

    delete(index) {
        this.index = index;
        this.delInfo = {
            "name": this.list[index].name,
            "mobile": this.list[index].mobile
        }
        this.showAlert = false;
    }

    //点击添加子账号
    addChildCount() {
        if (this.count > 0) {
            this.navCtrl.push(AddChildPage);
        } else {
            this.popup.alert("您可添加的账号已达到上限！");
        }
    }

    btnCancel() {
        this.showAlert = true;
    }

    btnChoose() {
        console.log(this.index);
        let param = {
            "id": this.list[this.index].id
        }
        this.childService.deleteChildAccount(param)
            .then(data => {
                if (data.resultStatus == 'success') {
                    console.log("删除成功");
                    this.onDidEnter();
                }
            }, err => {
                console.log(err);
                return err;
            })
            .catch(fail => {
                console.log(fail);
            });
        this.showAlert = true;
    }

    ngOnInit() {

    }


}


